<template>
	<div class="permissiomlistContainer" v-loading="loading">
		<el-table :data="searchs(search)" style="width: 100%" max-height="580">
			<el-table-column prop="id" label="ID" sortable width="80"></el-table-column>
			<el-table-column prop="name" label="权限名" sortable width="180"></el-table-column>
			<el-table-column prop="codename" sortable label="权限代码"></el-table-column>
			<el-table-column prop="content_type.app_label" sortable label="权限类型"></el-table-column>
			<el-table-column align="left" width="200">
				<template slot="header" slot-scope="scope">
					<span style="dispaly:none">{{scope.row}}</span>
					<el-input v-model="search" size="mini" placeholder="输入权限代码搜索" />
				</template>
				<template slot-scope="scope">
					<el-button
						v-if="canEdit"
						type="primary"
						size="mini"
						@click="handleEdit(scope.$index, scope.row)"
					>编辑</el-button>
					<el-button
						v-if="canDelete"
						size="mini"
						type="danger"
						@click="handleDelete(scope.$index, scope.row)"
					>删除</el-button>
				</template>
			</el-table-column>
		</el-table>
	</div>
</template>


<script>
export default {
	data() {
		return {
			search: "",
			canEdit: false,
			canDelete: false,
			tableData: [],
			user_infos: {},
			loading: false
		};
	},
	methods: {
		handleEdit(index, item) {
			this.$message({
				message: "暂未开放",
				type: "info"
			});
		},
		handleDelete(index, item) {
			this.$message({
				message: "暂未开放",
				type: "info"
			});
		},
		// 获取权限列表
		get_permissions() {
			this.$ajax.get("/permissions").then(res => {
				this.tableData = res.data;
				let that = this;
				setTimeout(function() {
					that.loading = false;
				}, 600);
			});
		},
		// 搜索功能
		searchs(kw) {
			let searchingList = [];
			this.tableData.forEach(item => {
				if (item.codename.indexOf(kw) != -1) {
					searchingList.push(item);
				}
			});
			return searchingList;
		}
	},
	mounted() {
		this.loading = true;
		this.get_permissions();
		this.user_infos = JSON.parse(window.localStorage.getItem("user"));
		if (this.user_infos.user_permissions.length > 0) {
			this.user_infos.user_permissions.forEach(item => {
				if (item.codename == "delete_bms_permission") {
					this.canDelete = true;
					return false;
				}
			});
			this.user_infos.user_permissions.forEach(item => {
				if (item.codename == "change_bms_permission") {
					this.canEdit = true;
					return false;
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
</style>